const { list, find, findByMovieInstanceId, deleteByMovieInstanceId, movieVersionList } = require('./data/movie')
const Mock = require('mockjs')

const base = '/movieserver'

module.exports = [{
  url: `${base}/movieInstances/total`,
  type: 'get',
  response: config => {
    return { success: true, data: Mock.Random.integer(800, 1000) }
  }
}, {
  url: `${base}/movieInstances/versions`,
  type: 'get',
  response: config => {
    const { page, limit, name, author, tag, movieLevelId, fileDeleted } = config.query
    return movieVersionList(page, limit, name, author, tag, movieLevelId, fileDeleted)
  }
}, {
  url: `${base}/movieInstances/movie/*/v/*/cover`,
  type: 'get',
  response: Mock.Random.image()
}, {
  url: `${base}/movieInstances/movie/[1-9][0-9]*/v/*`,
  type: 'get',
  response: config => {
    const id = config.url.match(/[1-9][0-9]*/)
    const r = find(id)
    return { success: true, data: r }
  }
}, {
  url: `${base}/movieInstances/[1-9][0-9]*/files`,
  type: 'delete',
  response: config => {
    const id = config.url.match(/[1-9][0-9]*/)
    const r = findByMovieInstanceId(id)
    if (r) {
      r.fileDeleted = 1
    }
    return { success: true }
  }
}, {
  url: `${base}/movieInstances/[1-9][0-9]*`,
  type: 'delete',
  response: config => {
    const id = config.url.match(/[1-9][0-9]*/)
    deleteByMovieInstanceId(id)
    return { success: true }
  }
}, {
  url: `${base}/movieInstances`,
  type: 'get',
  response: config => {
    const { page, limit, name, author, tag, movieLevelId, minPlayers, maxPlayers, minDuration, maxDuration } = config.query
    return list(page, limit, name, author, tag, movieLevelId, minPlayers, maxPlayers, minDuration, maxDuration)
  }
}, {
  url: `${base}/movies/[1-9][0-9]*/online`,
  type: 'put',
  response: config => {
    const id = config.url.match(/[1-9][0-9]*/)
    const r = find(id)
    if (r) {
      r.status = 0
      return { success: true }
    } else {
      return { success: false, message: '记录不存在' }
    }
  }
}, {
  url: `${base}/movies/[1-9][0-9]*/offline`,
  type: 'put',
  response: config => {
    const id = config.url.match(/[1-9][0-9]*/)
    const r = find(id)
    if (r) {
      r.status = 1
      return { success: true }
    } else {
      return { success: false, message: '记录不存在' }
    }
  }
}, {
  url: `${base}/movies/[1-9][0-9]*/price`,
  type: 'put',
  response: config => {
    const id = config.url.match(/[1-9][0-9]*/)
    const r = find(id)
    const { price } = config.body
    if (r) {
      r.price = parseFloat(price)
      return { success: true }
    } else {
      return { success: false, message: '记录不存在' }
    }
  }
}, {
  url: `${base}/movies/[1-9][0-9]*/level/[1-9][0-9]*`,
  type: 'put',
  response: config => {
    const arr = config.url.match(/[1-9][0-9]*/g)
    const r = find(arr[0])
    if (r) {
      r.movieLevelId = parseInt(arr[1])
      return { success: true }
    } else {
      return { success: false, message: '记录不存在' }
    }
  }
}]
